home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / tools / whereis.c
C/C++ Source or Header  |  1986-01-15  |  4KB  |  184 lines

  1.  
  2. Posted:  85-12-10 09:56:14 EST  Unread
  3. From:    techs                           
  4. Subject: Whereis.c -- find a file on the disk
  5. To:      amigados
  6. Text:
  7. /* WHEREIS.C
  8.  *   a program to search for files on any DOS disk.
  9.  *
  10.  * Syntax: WHEREIS [optional_root_node] <name-of-file>
  11.  *
  12.  */
  13.  
  14. /* when compiling, current drive must contain include directory */
  15. #include <:include/lattice/stdio.h>
  16. #include <:include/exec/types.h>
  17. #include <:include/libraries/dos.h>
  18.  
  19. main(argc,argv)
  20. int argc;
  21. char *argv[];
  22.  
  23. {
  24.       if ((argc < 2) || (argc > 3)) {
  25.          printf ("WHEREIS - Find a file anywhere on the default drive.\n");
  26.          printf ("Syntax: WHEREIS [optional_root_node] file_name_to_find\n");
  27.          exit (0);
  28.       }
  29.       if (argc == 2)                     /* use default drive, root node */
  30.          traverse(":", 1, argv[1]);
  31.       else if (last(argv[1]) == ':')
  32.          traverse(argv[1], 1, argv[2]);  /* user supplied drive name     */
  33.       else
  34.          traverse(argv[1], 2, argv[2]);  /* user supplied root node      */
  35. }
  36.  
  37.  
  38. traverse(head,depth,target)
  39. char head[];
  40. int depth;
  41. char target[];
  42.  
  43. {
  44. struct FileInfoBlock *info;
  45. long lock;
  46. char temp[100];
  47.  
  48.    if ((lock=Lock(head, SHARED_LOCK))==0) {
  49.       printf("Cannot open %s.\n", head);
  50.       return(0);
  51.    }
  52.  
  53. /* set up a lock for this node, and set up structure for first entry */
  54.  
  55.    info=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),0);
  56.    if (info==0) {
  57.       printf("Allocate Failed, depth=%ld\n",depth);
  58.       printf("Hacker meditation #555-1212\n");
  59.       UnLock(lock);
  60.       return(0);
  61.    }
  62.  
  63.    Examine(lock, info);
  64.  
  65. /* Examine each entry in this directory. If an entry is itself a     */
  66. /* directory, traverse that directory recursivly.                    */
  67.  
  68.    while (ExNext(lock, info)!= 0) {
  69.       strcpy(temp,head);
  70.       if (depth>1)
  71.          strcat(temp,"/");
  72.       strcat(temp,info->fib_FileName);
  73.       if (stringcmp(info->fib_FileName,target)==0)
  74.       printf("%s\n",temp);
  75.       if (info->fib_DirEntryType > 0)
  76.          traverse(temp, depth+1, target);
  77.    }
  78.  
  79.    UnLock(lock);
  80.    FreeMem(info,sizeof(struct FileInfoBlock));
  81. }
  82.  
  83. strcat(s,t)
  84. char *s,*t;
  85. {
  86.    while (*s != '\0')
  87.       *s++;
  88.    while (*s++=*t++)
  89.       ;
  90. }
  91.  
  92. strcpy(s,t)
  93. char *s,*t;
  94. {
  95.    while (*s++=*t++)
  96.       ;
  97. }
  98.  
  99.  
  100. stringcmp(s,t)
  101. char *s,*t;
  102. {
  103.    short int   pcount,
  104.                ecount;
  105.    char  patchar;
  106.  
  107.    pcount = ecount = 0;
  108.  
  109.    while(*s){
  110.       switch( *t){
  111.       case  '\0':
  112.             return( -1);
  113.       case  '\'':
  114.             if( ecount == 0){
  115.                ecount++;
  116.                t++;
  117.             }
  118.             else if( *s != *t)
  119.                   return(-1);
  120.             else{
  121.                   s++;
  122.                   t++;
  123.                   ecount = 0;
  124.             }
  125.             break;
  126.       case  '#':
  127.             if( ecount){
  128.                if( *s != '#')
  129.                   return( -1);
  130.                else{
  131.                   s++; t++; ecount = 0;
  132.                }
  133.             }
  134.             else{
  135.                pcount++;
  136.                t++;
  137.             }
  138.             break;
  139.       case  '?':
  140.             if( ecount){
  141.                if( *s != '?')
  142.                   return(-1);
  143.                else{
  144.                   s++;
  145.                   t++;
  146.                   ecount = 0;
  147.                }
  148.             }
  149.             else if( pcount){
  150.                   t++;
  151.                   while(*s && ( *s != *t)){
  152.                      s++;
  153.                   }
  154.                   pcount = 0;
  155.             }
  156.             else {
  157.                   t++; s++;
  158.             }
  159.             break;
  160.       default:
  161.             if( pcount){
  162.                patchar = *t++;
  163.                while(*s && ( *s == patchar))
  164.                   s++;
  165.                pcount = 0;
  166.             }
  167.             else if( *s++ != *t++)
  168.                return(-1);
  169.             break;
  170.       }
  171.    }
  172.  
  173.    return(*s - *t);
  174. }
  175.  
  176. last(s)
  177. char *s;
  178. {
  179.    while (*s != '\0')
  180.       s++;
  181.    return(*--s);
  182. }
  183.  
  184.